import  pymysql
class DatabaseTools:
    @classmethod
    def connect(cls):
        conn = pymysql.connect(host='211.103.136.244', port=7061, user='student', password='iHRM_student_2022',
                               database='ihrm', charset='utf8')
        # conn = pymysql.connect(host='localhost',port=3306,user='root',password='root',database='db_test1',charset='utf8')
        return conn

    @classmethod
    def select_database(cls,sql):

        cursor = None
        conn = None
        r = None
        try:
            conn = cls.connect()
            cursor= conn.cursor()
            cursor.execute(sql)
            r = cursor.fetchone()
        except Exception as e:
            print('发生了错误：',e)
        finally:
            cursor.close()
            conn.close()
            return r



    @classmethod
    def modify_database(cls,sql):
        cursor = None
        conn = None

        try:
            conn = DatabaseTools.connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
        except Exception as e:
            print('发生了错误：', e)
            conn.rollback()
        finally:
            cursor.close()
            conn.close()